package com.hornsun.repository;

import com.google.common.base.Preconditions;
import com.hornsun.domain.dbo.Medicine;
import com.hornsun.domain.dbo.MedicineExample;
import com.hornsun.repository.mapper.MedicineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by 编译中... on 2018/2/2.
 */
@Repository
public class MedicineRepository {
    @Autowired
    private MedicineMapper medicineMapper;

    public void insertMedicine(Medicine medicine){
        int count = medicineMapper.insertSelective(medicine);
        if (count!=1){
            throw new IllegalArgumentException("insert medicine failed by medicine:"+medicine);
        }
    }

    public void updateMedicine(Medicine medicine){
        Preconditions.checkNotNull(medicine);
        MedicineExample medicineExample = new MedicineExample();
        MedicineExample.Criteria criteria = medicineExample.createCriteria();
        criteria.andMedicineGuidEqualTo(medicine.getMedicineGuid());
        int count = medicineMapper.updateByExampleSelective(medicine,new MedicineExample());
        if (count!=1){
            throw new IllegalArgumentException("update medicine failed by medicine:"+medicine);
        }
    }
    public void deleteMedicine(String guid){
        Preconditions.checkNotNull(guid);
        int count = medicineMapper.deleteByPrimaryKey(guid);
        if (count!=1){
            throw new IllegalArgumentException("delete medicine failed by guid:"+guid);
        }

    }
    public List<Medicine> getMedicine(String name){
        Preconditions.checkNotNull(name);
        MedicineExample medicineExample = new MedicineExample();
        MedicineExample.Criteria criteria = medicineExample.createCriteria();
        criteria.andNameEqualTo(name);
        return medicineMapper.selectByExample(medicineExample);
    }

}
